<?php

declare(strict_types=1);

namespace app\index\controller;

use app\common\model\Wholesale as WholesaleModel;
use app\common\model\WholesaleOrder;
use app\common\model\WholesaleProduct;
use app\common\tool\Result;
use think\facade\View;
use think\Response;

class Wholesale extends Base
{
    /**
     *缔造首页
     */
    public function index()
    {
        return View::fetch();
    }
    /**
     * 获取数据
     */
    public function getList()
    {
        $param = $this->request->param();
        $map = ['status' => 1];
        $list = WholesaleModel::getPageList($map, 'id,title,start_time,end_time,type,images', $param['page'], 20, false, [], '', ['key' => 'id', 'value' => 'asc']);
        foreach ($list['data'] as &$item) {
            $stimes = strtotime(date('Y-m-d')) + time_to_sec($item->start_time);
            $etimes = strtotime(date('Y-m-d')) + time_to_sec($item->end_time);
            if ($stimes > time()) {
                $item->is_time = 1;
            } else if ($etimes < time()) {
                $item->is_time = 2;
            } else {
                $item->is_time = 0;
            }
        }
        return Result::fastReturn($list);
    }
    /**
     * 参团详情
     */
    public function info($id)
    {
        $info = WholesaleModel::find($id);
        View::assign('info', $info);
        View::assign('id', $id);
        return View::fetch();
    }
    public function getProductList()
    {
        $param = $this->request->param();

        $info = WholesaleModel::where(['id' => $param['id'], 'status' => 1])->find();
        if (empty($info)) {
            return Result::fastReturn(['code' => 1, 'msg' => '缔造已结束', 'data' => []]);
        }
        $map[] = ['id', 'in', explode(',', $info->product_id)];
        $list = WholesaleProduct::getPageList($map, 'id,title,bonus_2,bonus_4,toal', $param['page'], 20, false, [], '', ['key' => 'id', 'value' => 'asc']);
        foreach ($list['data'] as &$item) {
            $count = WholesaleOrder::where(['wholesale_id' => $param['id'], 'product_id' => $item['id']])->whereDay('create_time')->count();
            $my_count = WholesaleOrder::where(['uid' => $this->user->id])->whereDay('create_time')->count();
            if ($count >= $item->toal) {
                $item->is_pay = 1;
            } else {
                $item->is_pay = 0;
            }
            $item->is_buy = 1;
            if ($my_count > 0) {
                $item->is_buy = 0;
            }
            $stimes = strtotime(date('Y-m-d')) + time_to_sec($info->start_time);
            $etimes = strtotime(date('Y-m-d')) + time_to_sec($info->end_time);
            if ($stimes > time()) {
                $item->is_time = 1;
            } else if ($etimes < time()) {
                $item->is_time = 2;
            } else {
                $item->is_time = 0;
            }
            $item->is_bonus = $this->user->assets->bonus_5 >= 1 ? 1 : 0;
        }
        return Result::fastReturn($list);
    }
    /**
     * 添加缔造
     */
    public function submitBuy()
    {
        $param = $this->request->param();
        try {
            $info = WholesaleModel::where(['id' => $param['wid'], 'status' => 1])->find();
            if (empty($info)) {
                return Result::fastReturn(['code' => 1, 'msg' => '缔造不存在', 'data' => []]);
            }
            $stimes = strtotime(date('Y-m-d')) + time_to_sec($info->start_time);
            $etimes = strtotime(date('Y-m-d')) + time_to_sec($info->end_time);
            if ($stimes > time()) {
                return Result::fail('缔造还未开始');
            } else if ($etimes < time()) {
                return Result::fail('缔造已结束');
            }
            $find = WholesaleOrder::where(['uid' => $this->user->id])->where('create_time', '>', time() - 10)->count();
            if (!empty($find)) {
                return ['code' => -1, 'msg' => '您操作太频繁，请稍后再试。'];
            }
            return Result::fastReturn(WholesaleOrder::submitBuy($param, $this->user));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 我的参团
     */

    public function my_group_order()
    {
        return View::fetch();
    }

    public function my_order_details()
    {
        $info = WholesaleOrder::where(['uid' => $this->user->id, 'id' => $this->request->get('id')])
            ->find();
        if (empty($info)) return redirect('/');
        $info->statusCode = $info->getData('status');
        $info->friendlyText = getProductOrderFriendly($info->statusCode);
        View::assign('info', $info);
        return View::fetch();
    }

    public function getOrderList(): Response
    {
        try {
            $param = $this->request->get();
            $map = [['uid', '=', $this->user->id]];
            if (isset($param['type']) && $param['type']) {
                if ($param['type'] == 4) {
                    $map[] = ['free', '=', 1];
                } else {
                    $map[] = ['free', '=', 2];
                    $map[] = ['status', '=', $param['type']];
                }
            }
            $list = WholesaleOrder::getPageList(
                $map,
                '*',
                $this->getPage(),
                $this->getLimit(),
                false,
                ['product', 'wholesale'],
                '',
                '',
                '',
                '',
                '',
                ['status_text']
            );

            foreach ($list['data'] as &$v) {
                $v->statusCode = $v->getData('status');
                if ($v->free == 1) {
                    $v->status_texts = '缔造中';
                } else {
                    $v->status_texts = $v->status_text;
                }
                $v->create_times = date('Y-m-d', strtotime($v->create_time));
            }
            return Result::fastReturn($list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
